/**
 * Created by lester.liu(http://rossobe.com) 2013-10-10
 */

$(function($){
    $.browser = {};
    $.browser.mozilla = /firefox/.test(navigator.userAgent.toLowerCase());
    $.browser.msie    = /msie/.test(navigator.userAgent.toLowerCase());
    $.browser.msie7   =  /msie 7/.test(navigator.userAgent.toLowerCase());
    $.browser.msie8   =  /msie 8/.test(navigator.userAgent.toLowerCase());

    $.Body    = $('body');
    $.Window  = $(window);
    $.Scroll  = ($.browser.mozilla || $.browser.msie) ? $('html') : $.Body;

    $('[data-controller]').Instantiate();
});

(function($) {
    $.Events = {
        SECTION_ENTER: 'sectionEnter',
        SCROLL_TO: 'scrollTo',
        SCROLL: 'windowScroll',
        SCROLL_ENTER: 'windowScrollEnter',
        SCROLL_LEAVE: 'windwScrollLeave',
        KEY_UP: 'keyUp',
        KEY_DOWN: 'keyDown',
        KEY_LEFT: 'keyLeft',
        KEY_RIGHT: 'keyRight',
        KEY_ESC: 'keyEsc',
        KEY_SPACE: 'keySpace'
    };
    $.Views = {}

    $.fn.Instantiate = function(settings) {
        var config = {};
        if (settings) $.extend(config, settings);
        this.each(function(){
            var $self = $(this), $controller = $self.attr('data-controller');
            if ($self[$controller]) $self[$controller]();
        });

        _init_elem_height();
        $.Window.resize(function(){ _init_elem_height() });

        function _init_elem_height(){ }
    };

    $.fn.SiteMenuInit = function(){
        var $self = $(this), $li = $self.find('li.has-more'), $ext = $self.find('div.menu-ext');
        $li.each(function(i){
            $(this).bind('mouseenter', function(){
                $li.removeClass('active').eq(i).addClass('active')
                $ext.hide().eq(i).fadeIn()
            })
        })
        $self.bind('mouseleave', function(){ $li.removeClass('active'); $ext.hide() })
        $ext.bind('mouseleave', function(){ $ext.hide() })
    };

    $.fn.SiteMenuTabInit = function(){
        this.each(function(){
            var $self = $(this), $li = $self.find('.menu-ext-l li'), $ext = $self.find('.menu-ext-r ul');
            $li.each(function(i){
                $(this).bind('mouseenter', function(){
                    $li.removeClass('active').eq(i).addClass('active')
                    $ext.hide().eq(i).show()
                })
            })
            $li.filter(':first').trigger('mouseenter');
        })
    };

    $.fn.HomeProductTab = function(){
        this.each(function(){
            var $self = $(this); $li = $self.find('div.marquee li'), $item = $self.find('div.item');
            $li.each(function(i){
                $(this).bind('click', function(){
                    $li.removeClass('active').eq(i).addClass('active')
                    $item.hide().eq(i).show()
                })
            });
            $li.filter(':first').trigger('click')
        });
    };

    $.fn.ProductTab = function(){
        this.each(function(){
            var $self = $(this); $li = $self.find('div.marquee li'), $item = $self.find('div.item');
            $li.each(function(i){
                $(this).bind('click', function(){
                    $li.removeClass('active').eq(i).addClass('active')
                    $('#img4big').attr('src', $(this).attr('data-value'));
                })
            });
            $li.filter(':first').trigger('click')
        });
    };

    $.fn.CommonTab = function(){
        this.each(function(){
            var $self = $(this); $nav = $self.find('[data-value="nav"]'), $tab = $self.find('[data-value="tab"]');
            $nav.each(function(i){
                $(this).bind('click', function(){
                    $nav.removeClass('active').eq(i).addClass('active')
                    $tab.hide().eq(i).show();
                })
            });
            $nav.filter(':first').trigger('click')
        });
    };

})(jQuery);


var G = {};

G.ScollPostion = function() {
    var t, l, w, h;
    if (document.documentElement && document.documentElement.scrollTop) {
        t = document.documentElement.scrollTop;
        l = document.documentElement.scrollLeft;
        w = document.documentElement.scrollWidth;
        h = document.documentElement.scrollHeight;
    } else if (document.body) {
        t = document.body.scrollTop;
        l = document.body.scrollLeft;
        w = document.body.scrollWidth;
        h = document.body.scrollHeight;
    }
    return { top: t, left: l, width: w, height: h };
};
G.news_show = function(id){
    $('#news_detail').animate({ 'left': 0 }, 500, 'easeOutQuint');
};
G.news_paginate = function(page, self, target){
    var offset = -395;
    $(self).attr('class', 'pg_current').siblings().attr('class', 'pg_link');
    $(target).animate({top:offset*(page-1)});
};

G.dialog_close = function() {
    $.colorbox.close();
};

G.dialog_show = function(target) {
    var url = this.base_url + 'dialog.php?act=' + target;
    $.colorbox({fixed:true, overlayClose: false,scrolling: false,transition: 'none', fadeOut:0, href: url});
};

G.dialog_show_inline = function(target) {
    $.colorbox({fixed:true, overlayClose: false,scrolling: false,transition: 'none', fadeOut:0, href:target, inline:true});
};

G.error_handler = function(e) {
    if (e.content) {
        ET.dialog_show(e.content);
    } else {
        alert(e.message)
    }
};

